const CacheManager = require("../template")

function testCacheManagerBasic() {
  const cache = new CacheManager(10)

  console.log(
    "1. 基本缓存设置测试:",
    (() => {
      cache.set('key1', 'value1')
      const value = cache.get('key1')
      return value === 'value1'
    })()
  )

  console.log(
    "2. 缓存存在检查测试:",
    (() => {
      cache.set('key2', 'value2')
      const exists = cache.has('key2')
      const notExists = cache.has('key3')
      return exists === true && notExists === false
    })()
  )

  console.log(
    "3. 缓存删除测试:",
    (() => {
      cache.set('key4', 'value4')
      const beforeDelete = cache.has('key4')
      cache.delete('key4')
      const afterDelete = cache.has('key4')
      return beforeDelete === true && afterDelete === false
    })()
  )

  console.log(
    "4. 缓存大小测试:",
    (() => {
      cache.set('key5', 'value5')
      cache.set('key6', 'value6')
      const size = cache.size()
      return size === 2
    })()
  )

  console.log(
    "5. 缓存清空测试:",
    (() => {
      cache.set('key7', 'value7')
      cache.set('key8', 'value8')
      const sizeBefore = cache.size()
      cache.clear()
      const sizeAfter = cache.size()
      return sizeBefore === 2 && sizeAfter === 0
    })()
  )

  console.log(
    "6. 缓存键获取测试:",
    (() => {
      cache.set('key9', 'value9')
      cache.set('key10', 'value10')
      const keys = cache.keys()
      return keys.length === 2 && keys.includes('key9') && keys.includes('key10')
    })()
  )

  console.log(
    "7. 缓存值获取测试:",
    (() => {
      cache.set('key11', 'value11')
      cache.set('key12', 'value12')
      const values = cache.values()
      return values.length === 2 && values.includes('value11') && values.includes('value12')
    })()
  )

  console.log(
    "8. 缓存条目获取测试:",
    (() => {
      cache.set('key13', 'value13')
      cache.set('key14', 'value14')
      const entries = cache.entries()
      return entries.length === 2 &&
             entries.some(entry => entry[0] === 'key13' && entry[1] === 'value13') &&
             entries.some(entry => entry[0] === 'key14' && entry[1] === 'value14')
    })()
  )

  console.log(
    "9. 最大大小设置测试:",
    (() => {
      cache.setMaxSize(5)
      const maxSize = cache.getMaxSize()
      return maxSize === 5
    })()
  )

  console.log(
    "10. 缓存统计测试:",
    (() => {
      cache.set('key15', 'value15')
      cache.get('key15')
      cache.get('key16') // 不存在的键
      const stats = cache.getStats()
      return stats.hits >= 1 && stats.misses >= 1 && stats.sets >= 1
    })()
  )

  console.log(
    "11. this上下文缓存管理器测试:",
    (() => {
      const cacheObj = {
        multiplier: 2,
        processCache: function(key, value) {
          const cache = new CacheManager()
          cache.set(key, value)
          return cache.get(key) * this.multiplier
        }
      }
      return cacheObj.processCache('test', 5) === 10
    })()
  )

  console.log(
    "12. 复杂对象this绑定缓存管理器测试:",
    (() => {
      const cacheConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(key, value) {
          const cache = new CacheManager()
          cache.set(key, value)
          return cache.get(key) >= this.config.threshold
        }
      }
      return cacheConfigObj.processWithConfig('test', 5) === true
    })()
  )
}

testCacheManagerBasic()
